import { HttpService } from "src/app/shared/service/http.service";
import { Component, OnInit, Input } from "@angular/core";
import { AppGlobalService } from "src/app/shared/service/app-global.service";
import { SearchListService } from "src/app/shared/service/search-list.service";
import { isArray } from "util";

@Component({
  selector: "app-search-input",
  templateUrl: "./search-input.component.html",
  styleUrls: ["./search-input.component.scss"]
})
export class SearchInputComponent implements OnInit {
  @Input() tagData: any;

  value: string; // 输入值

  timer = null; // 定时器

  constructor(
    private http: HttpService,
    private appGlobalService: AppGlobalService,
    private broadcast: SearchListService
  ) {}

  ngOnInit() {}

  onBlur() {
    // 场景1：提交搜索，触发submit
    if (this.tagData.search.button && isArray(this.tagData.search.button)) {
      if (this.timer) {
        clearTimeout(this.timer);
      }
      this.timer = setTimeout(() => {
        const data = {};
        // 参数赋值
        for (const key in this.tagData.search.button[0].submit.content) {
          if (key && !this.tagData.search.button[0].submit.content[key]) {
            data[key] = this.value || '';
          }
        }

        // 整合参数
        const params = {
          content: data,
          id: this.tagData.search.button[0].submit.id,
          sessionId: localStorage.getItem("sessionId")
        };

        // 广播请求
        this.http.post("application/submit", params, res => {
          this.broadcast.sendMessage({
            type: "submit",
            id: this.tagData.search.button[0].submit.id,
            value: res
          });
        });

        const obj = {};
        // 获取按钮参数
        if (this.tagData.search.button[1]) {
          for (const key in this.tagData.search.button[1].submit.content) {
            if (key && !this.tagData.search.button[1].submit.content[key]) {
              obj[key] = this.value;
            }
          }
          // 整合参数
          const param = {
            content: obj,
            id: this.tagData.search.button[1].submit.id,
            sessionId: localStorage.getItem("sessionId")
          };
          // 广播请求
          this.http.post("application/submit", param, res => {
            this.broadcast.sendMessage({
              type: "submit",
              id: this.tagData.search.button[1].submit.id,
              value: res
            });
          });
        }
      }, 300);
    } else if (this.tagData.search.button && this.tagData.search.button.link) {
      // 场景2：带参数跳转
      // 整合请求参数
      const pageParams = {
        pageId: this.tagData.search.button.link.target,
        params: this.tagData.search.button.link.params,
        sessionId: localStorage.getItem("sessionId")
      };
      const obj = this.tagData.search.paramKey.split(",");
      obj.map(v => {
        pageParams.params[v] = this.value || '';
      });

      // 跳转页面
      this.appGlobalService.goToForward(pageParams);
    } else {
      // 场景3：搜索本标签，不跳转，不提交，通过key值进行前端找json值
      this.broadcast.sendMessage({ type: "filter", value: this.value });
    }
  }
}
